package com.yingsheng.bi.hks.service.employee;

import com.yingsheng.bi.hks.basic.PUser;
import com.yingsheng.bi.hks.basic.enums.EnumConst;
import com.yingsheng.bi.hks.basic.enums.jobs.EnumEmployee;
import com.yingsheng.bi.hks.basic.utils.Subject;
import com.yingsheng.bi.hks.basic.utils.Utils;
import com.yingsheng.bi.hks.bo.ResumeBo;
import com.yingsheng.bi.hks.bo.WorkExperience;
import com.yingsheng.bi.hks.dto.employee.ResumeAddReq;
import com.yingsheng.bi.hks.dto.employee.ResumeAddRes;
import com.yingsheng.bi.hks.dto.employee.ResumeQryReq;
import com.yingsheng.bi.hks.dto.employee.ResumeQryRes;
import com.yingsheng.bi.hks.repository.dao.po.*;
import com.yingsheng.bi.hks.repository.dao.service.*;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class EmployeeMgmtService {

    @Autowired
    private ResumeDetailsDaoService resumeDetailsDaoService;

    @Autowired
    private ResumeExceptsDaoService resumeExceptsDaoService;

    @Autowired
    private ResumeInfoDaoService resumeInfoDaoService;

    @Autowired
    private ResumeWorksDaoService resumeWorksDaoService;

    @Autowired
    private UsersDaoService usersDaoService;

    public ResumeAddRes add(ResumeAddReq req) {

        PUser user = Subject.get();

        ResumeAddRes res = new ResumeAddRes();
        String resumeInfoId = Utils.genUUid();
        Date createDate = new Date();
        ResumeInfo resumeInfo = new ResumeInfo();
        resumeInfo.setId(resumeInfoId);
        resumeInfo.setUserId(user.getId());
        resumeInfo.setWorkStatus(req.getWorkStatus());
        resumeInfo.setName(req.getName());
        resumeInfo.setMobile(user.getMobile());
        resumeInfo.setAvatar(req.getAvatar());
        resumeInfo.setDescription(req.getDescription());
        resumeInfo.setEducation(req.getEducation());
        resumeInfo.setCreateTime(createDate);

        List<ResumeDetails> resumeDetailsList = new ArrayList<>();
        for (EnumEmployee enumEmployee : EnumEmployee.values()) {
            ResumeDetails resumeDetails = new ResumeDetails();
            resumeDetails.setId(Utils.genUUid());
            resumeDetails.setResumeId(resumeInfoId);
            resumeDetails.setFieldId(enumEmployee.getValue());
            resumeDetails.setFieldName(enumEmployee.name());
            try {
                Field nameField = ResumeAddReq.class.getDeclaredField(enumEmployee.name());
                nameField.setAccessible(true); // 取消Java的访问控制检查
                String nameValue = (String) nameField.get(req);
                resumeDetails.setFieldValue(nameValue);
            }catch (Exception e){
                res.setErrorCode("999999");
                res.setErrorCode(e.getMessage());
                return res;
            }
            resumeDetails.setCreateTime(createDate);
            resumeDetailsList.add(resumeDetails);
        }

        ResumeExpects resumeExpects = new ResumeExpects();
        resumeExpects.setId(Utils.genUUid());
        resumeExpects.setResumeId(resumeInfoId);
        resumeExpects.setPostsion(req.getPosition());
        resumeExpects.setCreateTime(createDate);

        List<ResumeWorksWithBLOBs> resumeWorksWithBLOBsList = new ArrayList<>();
        if (!req.getWorkExperiences().isEmpty()){
            for (int i = 0; i < req.getWorkExperiences().size(); i++) {
                WorkExperience workExperience = req.getWorkExperiences().get(i);
                ResumeWorksWithBLOBs resumeWorksWithBLOBs = new ResumeWorksWithBLOBs();
                resumeWorksWithBLOBs.setId(Utils.genUUid());
                resumeWorksWithBLOBs.setResumeId(resumeInfoId);
                resumeWorksWithBLOBs.setBegDate(workExperience.getBegDate());
                resumeWorksWithBLOBs.setEndDate(workExperience.getEndDate());
                resumeWorksWithBLOBs.setPosition(workExperience.getPosition());
                resumeWorksWithBLOBs.setContent(workExperience.getContent());
                resumeWorksWithBLOBsList.add(resumeWorksWithBLOBs);
            }

        }

        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(EnumConst.Role.P04);

        resumeInfoDaoService.insert(resumeInfo);
        resumeDetailsDaoService.insert(resumeDetailsList);
        resumeExceptsDaoService.insert(resumeExpects);
        resumeWorksDaoService.insert(resumeWorksWithBLOBsList);
        usersDaoService.updateUserRoleById(userRole);

        return res;
    }

    public ResumeQryRes qry(ResumeQryReq req) {

        ResumeQryRes res = new ResumeQryRes();
        List<ResumeBo> resumeBoList = new ArrayList<>();
        List<ResumeInfo> resumeInfoList = resumeInfoDaoService.qryByReq(req);
        if (resumeInfoList.isEmpty()){
            return res;
        }
        for (int i = 0; i < resumeInfoList.size(); i++) {
            ResumeBo resumeBo = new ResumeBo();
            ResumeInfo resumeInfo = resumeInfoList.get(i);
            resumeBo.setId(resumeInfo.getId());
            resumeBo.setWorkStatus(req.getWorkStatus());
            resumeBo.setName(req.getName());
            resumeBo.setMobile(req.getMobile());
            resumeBo.setAvatar(resumeInfo.getAvatar());
            resumeBo.setDescription(resumeInfo.getDescription());
            resumeBo.setEducation(req.getEducation());

            List<ResumeDetails> resumeDetailsList = resumeDetailsDaoService.query(resumeInfo.getId());
            for (int j = 0; j < resumeDetailsList.size(); j++) {

                ResumeDetails resumeDetails = resumeDetailsList.get(j);
                try {
                    Field nameField = ResumeBo.class.getDeclaredField(resumeDetails.getFieldName());
                    nameField.setAccessible(true); // 取消Java的访问控制检查
                    nameField.set(resumeBo,resumeDetails.getFieldValue());
                }catch (Exception e){
                    res.setErrorCode("999999");
                    res.setErrorCode(e.getMessage());
                    return res;
                }
            }

            ResumeExpects resumeExpects = resumeExceptsDaoService.qryByResumeInfoId(resumeInfo.getId());
            if (!StringUtil.isNullOrEmpty(resumeExpects.getPostsion())){
                resumeBo.setPosition(resumeExpects.getPostsion());
            }

            List<WorkExperience> workExperienceList = new ArrayList<>();
            List<ResumeWorksWithBLOBs> resumeWorksList = resumeWorksDaoService.qryByResumeInfoId(resumeInfo.getId());
            if (!resumeWorksList.isEmpty()){
                for (int j = 0; j < resumeWorksList.size(); j++) {
                    WorkExperience workExperience = new WorkExperience();
                    ResumeWorksWithBLOBs resumeWorksWithBLOBs = resumeWorksList.get(j);
                    workExperience.setPosition(resumeWorksWithBLOBs.getPosition());
                    workExperience.setContent(resumeWorksWithBLOBs.getContent());
                    workExperience.setBegDate(resumeWorksWithBLOBs.getBegDate());
                    workExperience.setEndDate(resumeWorksWithBLOBs.getEndDate());
                    workExperienceList.add(workExperience);
                }
            }
            resumeBo.setWorkExperiences(workExperienceList);
            resumeBoList.add(resumeBo);
        }
        res.setResumeBoList(resumeBoList);
        return res;
    }
}
